package com.itheima.service.impl;


import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.CheckItemDao;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import redis.clients.jedis.JedisPool;

import java.util.List;

@Service(interfaceClass = CheckItemService.class)
@Transactional
public class CheckItemServiceImpl implements CheckItemService{

    @Autowired
    private CheckItemDao checkItemDao;
    @Autowired
    private JedisPool jedisPool;

    private Logger logger = LoggerFactory.getLogger(SetmealServiceImpl.class);

    /**
     * @author wu_zh
     * @date 2019/7/1 15:29
     * @param
     * @param checkItem
     * @return void
     * @Description 添加检查项
     */
    @Override
    public void add(CheckItem checkItem) {
        checkItemDao.add(checkItem);
        jedisPool.getResource().flushDB();
        logger.debug("添加检查项操作后清空redis");
    }

    @Override
    public PageInfo<CheckItem> findPage(QueryPageBean queryPageBean) {

        if (!StringUtils.isEmpty(queryPageBean.getQueryString())){
            queryPageBean.setQueryString("%"+queryPageBean.getQueryString()+"%");
        }

        // 分页插件
        //获取第1页，10条内容，默认查询总数count
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        //紧跟着的第一个select方法会被分页，查询所有
//        Page<CheckItem> checkItems = checkItemDao.findByCondition(queryPageBean.getQueryString());
//        System.out.println("========================"+checkItems.getTotal());
        List<CheckItem> checkItems = checkItemDao.findByCondition(queryPageBean.getQueryString());
        PageInfo pageInfo = new PageInfo(checkItems);
        return pageInfo;
    }

    @Override
    public CheckItem editId(int id) {
        return checkItemDao.editId(id);
    }

    /**
     * @author wu_zh
     * @date 2019/7/1 15:29
     * @param
     * @param checkItem
     * @return void
     * @Description 更新检查项
     */
    @Override
    public void updateCheckItem(CheckItem checkItem) {
        checkItemDao.updateCheckItem(checkItem);
        jedisPool.getResource().flushDB();
        logger.debug("更新检查项操作后清空redis");
    }

    /**
     * @author wu_zh
     * @date 2019/7/1 15:29
     * @param
     * @param id
     * @return void
     * @Description 删除检查项
     */
    @Override
    public void deleteById(int id) {
        //查看是否有被引用
        int count = checkItemDao.findByCheckId(id);
        if (count>0){
            throw new RuntimeException("有引用,不能删除");
        }
        checkItemDao.deleteById(id);
        jedisPool.getResource().flushDB();
        logger.debug("删除检查项操作后清空redis");
    }
}
